//
//  YDYComment.m
//  DYSisterAPP
//
//  Created by 杨德勇 on 16/8/4.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYTopic.h"
#import "YDYUser.h"
#import "YDYComment.h"
@implementation YDYTopic




-(NSString *)created_at{
//    _created_at = @"2015-08-05 12:48:50";
    //拿到日历
    NSCalendar *calendar = [NSCalendar ydy_calendar];
    
    //获取发帖日期
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *createDate = [formatter dateFromString:_created_at];;
   
    
    if (createDate.ydy_isThisYear) {//今年
        if ([calendar isDateInToday:createDate]) {//今天
             //获取当前时间
            NSDate *nowdate = [NSDate date];
            //获取时间差
            NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
            NSDateComponents *cps = [calendar components:unit fromDate:createDate toDate:nowdate options:0];
            
            
            if (cps.hour >= 1) {//大于一小时
                return [NSString stringWithFormat:@"%ld小时前",(long)cps.hour];
            }else if (cps.minute >= 1){//大于一分钟
                return [NSString stringWithFormat:@"%ld分钟前",(long)cps.minute];
            }else{ //小于一分钟
                return @"刚刚";
            }
            

            
            
        }else if ([calendar isDateInYesterday:createDate]){//昨天
            formatter.dateFormat = @"昨天 hh:mm ";
            return [formatter stringFromDate:createDate];
        }else {//其他(是今年)
            NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
            //获取月数
            NSDateComponents  *date =  [calendar components:unit fromDate:createDate];
            return [NSString stringWithFormat:@"%ld月%ld日 %ld:%2ld",(long)date.month,(long)date.day,(long)date.hour,(long)date.minute];
            
        }
        
    }else{
        //不是今年的
        return _created_at;
    }
    
    
    
    return _created_at;
}

-(CGFloat)cellHeight{
    //性能优化
    if (_cellHeight)return _cellHeight;
    
    //头像高度78,xib写死的 label的Y值
    _cellHeight = 78 + YDYCellMargin ;
    //文字内容高度
    CGFloat textMaxW =[UIScreen mainScreen].bounds.size.width -28;
    CGSize textMaxSize =CGSizeMake(textMaxW, MAXFLOAT);
//    CGSize textSize =  [self.text sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:textMaxSize];
    CGSize textSize =  [self.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName :[UIFont systemFontOfSize:17]} context:nil].size;
    
    _cellHeight += textSize.height + YDYCellMargin;
    //中间内容
    if (self.type != YDYTopicTypeWord ) {//除去文字类型的
        //中间内容的高度
        CGFloat contentH = textMaxW * self.height /self.width;
        if (contentH > [UIScreen mainScreen].bounds.size.height *0.6) {
            contentH = [UIScreen mainScreen].bounds.size.height *0.6;
            self.bigPicture = YES;
        }
        //中间内容的frame
        self.contentF =  CGRectMake(YDYCellMargin, _cellHeight, textMaxW, contentH);
        //加上中间内容高度
        _cellHeight += contentH + YDYCellMargin;
    }
    //最热评论高度
    if (self.top_cmt) {
        //最热评论那4歌字的高度
        _cellHeight += 16;
        //最热评论的内容的高度
        NSString *content = self.top_cmt.content;
            //判断是不是语音评论，如果是在内容上面补几个字
        if (self.top_cmt.voiceuri.length) {
            content = @"语音评论";
        }
        NSString *cmtTextContent  = [NSString stringWithFormat:@"%@:%@",self.top_cmt.user.username,content];
//        CGSize topCmtSize = [cmtTextContent sizeWithFont:[UIFont systemFontOfSize:11] constrainedToSize:textMaxSize];
        CGSize topCmtSize = [cmtTextContent boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11]} context:nil].size;
        _cellHeight += topCmtSize.height + YDYCellMargin;
    
        

    }
    //工具条高度 + margin
    _cellHeight += 30 + YDYCellMargin;
    return _cellHeight;
}
@end
